package com.red.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.red.dto.HomePageQueryDTO;
import com.red.dto.ProductsDTO;
import com.red.entity.Category;
import com.red.entity.Products;
import com.red.result.PageResult;
import com.red.result.Result;
import com.red.service.ProductsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("ProductController")
@RequestMapping("/admin/product")
@Slf4j
@CrossOrigin
public class ProductController {
    @Autowired
    private ProductsService productsService;
    /**
     * 分页查询
     * @param dto
     * @return
     */
    @GetMapping("/pageQuery")
    public Result<PageResult> pageQuery(HomePageQueryDTO dto) {
        return Result.success(productsService.pageQuery(dto));
    }

    /**
     * 新增商品
     * @param dto
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Products dto) {
        productsService.insert(dto);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Long> ids) {
        log.info("delete ids : {}", ids);
        productsService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 更新商品信息
     * @param dto
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody ProductsDTO dto) {
        log.info("update dto: {}", dto);
        productsService.updateProducts(dto);
        return Result.success();
    }

    /**
     * 查询所有分类
     * @return
     */
    @GetMapping("/selectCategory")
    public Result<List<Category>> selectCategory(){
        List<Category> list = productsService.selectList();
        return Result.success(list);
    }

    /**
     * 更新分类
     * @param category
     * @return
     */
    @PostMapping("/updateCategory")
    public Result updateCategory(@RequestBody Category category){
        productsService.updateCategory(category);
        return Result.success();
    }

    /**
     * 分类删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteCategory/{ids}")
    public Result deleteCategory(@PathVariable List<Long> ids){
        productsService.deleteCategoryByIds(ids);
        return Result.success();
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping("/insertCategory")
    public Result insertCategory(@RequestBody Category category){
        productsService.insrtCategory(category);
        return Result.success();
    }
}
